package com.store.orm.mysql.datasource;

import com.store.orm.config.MySQLConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

/**
 * 获取数据库连接
 *
 * @author 宫静雨
 * @version 1.0
 */
public class UnPooledConnection {
    private static final Logger logger = LoggerFactory.getLogger(UnPooledConnection.class);

    public Connection get(MySQLConfig config) {
        try {
            Class.forName(config.getDriver());
            return DriverManager.getConnection(config.getUrl(), config.getUsername(), config.getPassword());
        } catch (SQLException | ClassNotFoundException e) {
            logger.error("{}", e.getMessage());
            return null;
        }
    }

    public Connection get(Map<String, MySQLConfig> map, String key) {
        try {
            MySQLConfig config = map.get(key);
            return DriverManager.getConnection(config.getUrl(), config.getUsername(), config.getPassword());
        } catch (SQLException e) {
            logger.error("{}", e.getMessage());
            return null;
        }
    }
}